package com.ara.websocket;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

/**
 * Created by XieXin on 2021/7/5.
 * 基础WebSocket客户端
 */
public class BaseWebSocketClient extends WebSocketClient {
    private static final String TAG = BaseWebSocketClient.class.getSimpleName();

    public BaseWebSocketClient(URI serverUri) {
        super(serverUri, new Draft_6455());
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        Log.i(TAG, "=======> WebSocket连接成功" +
                " HttpStatus: " + serverHandshake.getHttpStatus() +
                " HttpStatusMessage: " + serverHandshake.getHttpStatusMessage()
        );
    }

    @Override
    public void onMessage(String message) {
        Log.i(TAG, "=======> WebSocket接收消息: " + message);
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        Log.d(TAG, "=======> WebSocket连接取消" +
                " code: " + code +
                " reason: " + reason +
                " remote: " + remote
        );
    }

    @Override
    public void onError(Exception ex) {
        Log.e(TAG, "=======> WebSocket连接异常: " + ex);
    }
}
